package com.the4man.cnbdw.service.impl;

import com.the4man.cnbdw.dao.MessageMapper;
import com.the4man.cnbdw.dao.MessageRecordMapper;
import com.the4man.cnbdw.dao.MessageReplyMapper;
import com.the4man.cnbdw.exception.BusinessException;
import com.the4man.cnbdw.service.MessageService;
import com.the4man.framework.model.*;
import com.the4man.framework.utils.GenUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * Created by lenovo on 2017/12/12.
 */
@Service
public class MessageServiceImpl implements MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MessageReplyMapper messageReplyMapper;
    @Autowired
    private MessageRecordMapper messageRecordMapper;

    public MessageServiceImpl() {
    }

    public int getMsgCnt(String keyWord) throws BusinessException, Exception {
        MessageExample example = new MessageExample();
        if(StringUtils.isNotEmpty(keyWord)) {
            MessageExample.Criteria criteria = example.createCriteria();
            criteria.andTitleContentOR("%" + keyWord + "%");
        }

        int count = this.messageMapper.countByExample(example);
        return count;
    }

    public List<Message> getMsgPage(int startRows, int pageSize, String keyWord, String name, String start, String end) throws BusinessException, Exception {
        MessageExample example = new MessageExample();
        example.setStartRow(startRows);
        example.setPageSize(pageSize);
        MessageExample.Criteria criteria;
        if(StringUtils.isNotEmpty(keyWord)) {
            criteria = example.createCriteria();
            criteria.andTitleContentOR("%" + keyWord + "%");
        }

        if(StringUtils.isNotEmpty(name)) {
            criteria = example.createCriteria();
            criteria.andMsgCreateUserEqualTo(name);
        }

        if(StringUtils.isNotEmpty(start) || StringUtils.isNotEmpty(end)) {
            criteria = example.createCriteria();
            criteria.andMsgCreateTimeBetween(start, end);
        }

        example.setOrderByClause("msg_create_time desc");
        List<Message> list = this.messageMapper.selectByExample(example);
        return list;
    }

    public boolean addSendMessage(Message msg, List<String> recIdList) throws BusinessException, Exception {
        int msgRows = this.messageMapper.insert(msg);
        int msgRecordCount = 0;

        int upRows;
        for(Iterator var6 = recIdList.iterator(); var6.hasNext(); msgRecordCount += upRows) {
            String recId = (String)var6.next();
            MessageRecord msgRecord = new MessageRecord();
            msgRecord.setRecordId(GenUtil.genUUID());
            msgRecord.setMsgId(msg.getMsgId());
            msgRecord.setIsRead("0");
            msgRecord.setRecUser(recId);
            upRows = this.messageRecordMapper.insert(msgRecord);
        }

        if(msgRows >= 1 && msgRecordCount >= 1) {
            return true;
        } else {
            throw new Exception("发送消息时，主信息或发送记录添加失败!");
        }
    }

    public boolean deleteMessage(List<String> msgIdList) throws BusinessException, Exception {
        Iterator var3 = msgIdList.iterator();

        while(var3.hasNext()) {
            String msgId = (String)var3.next();
            MessageReplyExample example1 = new MessageReplyExample();
            MessageReplyExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andMsgIdEqualTo(msgId);
            this.messageReplyMapper.deleteByExample(example1);
            MessageRecordExample example2 = new MessageRecordExample();
            MessageRecordExample.Criteria criteria2 = example2.createCriteria();
            criteria2.andMsgIdEqualTo(msgId);
            this.messageRecordMapper.deleteByExample(example2);
            this.messageMapper.deleteByPrimaryKey(msgId);
        }

        return true;
    }

    public List<MessageReply> getRepllyList(String msgId) throws BusinessException, Exception {
        MessageReplyExample example1 = new MessageReplyExample();
        MessageReplyExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andMsgIdEqualTo(msgId);
        return this.messageReplyMapper.selectByExample(example1);
    }

    public int addMsg(Message message) {
        return this.messageMapper.insertMsg(message);
    }

    public List<Message> selectALLMsg() {
        return this.messageMapper.selectAllMsg();
    }
}
